package com.java1234.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.swing.JTable;

import com.java1234.dao.IReturnBookDao;
import com.java1234.model.Borrow;
import com.java1234.model.ReturnBook;
import com.java1234.util.DbUtil;
import com.java1234.util.StringUtil;
import com.java1234.view.BookReturnManageInterFrm;

public class ReturnBookImpl implements IReturnBookDao {
	private DbUtil dbUtil = new DbUtil();// 只有dbutil，想拿con，就dbUtil.getCon()
	private int n=0;
	/**
	 * 查询图书借阅表集合
	 * 
	 * @param con
	 * @param bookType
	 * @return
	 * @throws Exception
	 */
	public ResultSet list(Connection con, ReturnBook returnBook) throws Exception {
		StringBuffer sb = new StringBuffer("select * from t_returnBook");
		if (StringUtil.isNotEmpty(returnBook.getBorrowerAccount())) {
			sb.append(" and borrowerAccount like '%" + returnBook.getBorrowerAccount() + "%'");
		}
		PreparedStatement pstmt = con.prepareStatement(sb.toString().replaceFirst("and", "where"));
		ResultSet i = pstmt.executeQuery();
		return i;

	}

	/**
	 * 数据库还书（删除借阅表信息）并修改借阅表借书信息（借阅者为空，图书状态改为在馆）
	 * 
	 * @param con
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public int returnBook(Connection con, ReturnBook returnBook) throws Exception {
		String sql = "update t_borrow set bookStatus=? where bookId=?";
		PreparedStatement pstmt = con.prepareStatement(sql);

		pstmt.setString(1, "在馆");
		pstmt.setString(2, returnBook.getBookId());
		int i = pstmt.executeUpdate();

		return i;
	}

	/**
	 * 数据库借阅信息删除
	 * 
	 * @param con
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public int delete(Connection con, String bookId) throws Exception {
		String sql = "delete from t_returnbook where bookId=?";
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setString(1, bookId);
		int i = pstmt.executeUpdate();

		return i;

	}

	/**
	 * 数据库续借图书
	 * 
	 * @param con
	 * @param book
	 * @return
	 * @throws Exception
	 */
	public int update(Connection con, ReturnBook returnBook) throws Exception {
		String sql = "update t_returnbook set returnTime=?,status=? where bookId=?";
		PreparedStatement pstmt = con.prepareStatement(sql);

		Date returnTime = returnBook.getReturnTime();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Calendar c = Calendar.getInstance();
		c.setTime(returnTime);

		c.add(Calendar.MONTH, +1);
		Date m = c.getTime();
		String endTime = format.format(m);
		if(n<2) {
			n++;
		}
		pstmt.setDate(1, java.sql.Date.valueOf(endTime));
		pstmt.setString(2, "续借"+n+"次");
		pstmt.setString(3, returnBook.getBookId());
		int i = pstmt.executeUpdate();

		return i;

	}

	/**
	 * 数据库超期罚款（按归还按钮时计算，一次算完，有对话框提示“请先支付罚款”）
	 * 
	 * @param con
	 * @param book
	 * @return
	 * @throws Exception
	 */
	public int fine(Connection con, ReturnBook returnBook) throws Exception {
		String sql = "select returnTime,fine from t_returnbook where bookId=?";
		PreparedStatement pstmt = con.prepareStatement(sql);
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

		Calendar cal1 = Calendar.getInstance();
		cal1.setTime(new Date());

		Calendar cal2 = Calendar.getInstance();
		cal2.setTime(returnBook.getReturnTime());

		Date date2 = cal2.getTime();
		String returnTime = format.format(date2);

		long day1 = cal1.get(Calendar.DAY_OF_YEAR);
		long day2 = cal2.get(Calendar.DAY_OF_YEAR);
		System.out.println("超期天数:" + (day2 - day1) + "超期罚款:" + (day2 - day1) * 2);
		int fine = (int) ((day2 - day1) * 2);

		pstmt.setDate(1, java.sql.Date.valueOf(returnTime));
		pstmt.setInt(1, fine);
		pstmt.setString(1, returnBook.getBookId());
		int i = (int) ((day2 - day1) * 2);

		return i;

	}

	/**
	 * 数据库罚款清零
	 * 
	 * @param con
	 * @param book
	 * @return
	 * @throws Exception
	 */
	public int fineZero(Connection con, ReturnBook returnBook) throws Exception {
		String sql = "update t_returnbook set fine=? where bookId=?";
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setInt(1, 0);
		pstmt.setString(2, returnBook.getBookId());
		int i = pstmt.executeUpdate();
		return i;
	}

}
